define(function(require, exports, module) {
	var $ = require("$");
	var bootbox = require("bootbox");
	var dtUtil = require('datatableutil');
	var loader = require("./loader/loader");
	require("bootstrap_commonrequire");
	require("moment");
	require('daterangepicker.css');
	require('bootstrap-daterangepicker');
	require('select2.css');
	require('select2');

	exports.init = function(param) {
		$(document).ready(
			function() {
				loader.loading("数据加载中……");
				// 加载自定义模板-条件区域
				var template = require("./accountcompanyinfo.tpl");
				var Widget = require("widget");
				var Component = Widget.extend({
					template : template,
				});
				new Component({
					renderTo : "queryAreaId"
				});

				function getCondition() {
					// 获取查询条件
					var isv_name = $('#isvname').val();
					var queryStr = "";
					if (isv_name != '' && isv_name != null) {
						queryStr += ("isv_name=" + isv_name + ";");
					}
					// 获取组织id
					var app_name = $('#app_name').val();// 企业名称
					if (app_name != '' && app_name != null) {
						queryStr += ("app_name=" + app_name + ";");
					}

					// 获取app查询信息
					var cust_account = $('#cust_account').val();
					if (cust_account != '' && cust_account != null) {
						queryStr += ("cust_account=" + cust_account + ";");
					}
					return queryStr;
				}
				// 加载表单模板和数据
				var Table = require("table");
				// 查询内容显示
				new Table({
					attrs : {
						id : "data_table",
						isHovered : false,
						isStriped : true,
						isBordered : false
					},
					renderTo : param.divWrapperId

				});
				// isv信息显示
				var isvinfo_data_table = $('#data_table').dataTable(
					{
						"sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
						"bJQueryUI" : false,
						"oLanguage" : {
							"sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
						},
						"sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
						'bPaginate' : true, // 是否分页
						"bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
						"bServerSide" : true, // 指定服务器端分页
						"bDeferRender" : true,
						"bFilter" : true,
						"bSort" : true,
						"bInfo" : true,
						"sAjaxSource" : param.ajaxUrl,
						"iDisplayLength" : 5, // DATATABLES_CONFIG.iDisplayLength.MIN
												// 默认分页的每页记录数
						"fnServerData" : function(sSource, aoData,fnCallback, oSettings) {
							var queryparam = dtUtil.convert_aoData(aoData);
							// 拼接where查询条件
							var cond = getCondition();
							if (cond != '' && cond != null) {
								queryparam.q += cond;
							}

							// 添加自己的额外参数
							oSettings.jqXHR = $.ajax({
								"dataType" : 'json',
								"contentType" : "application/json;charset=UTF-8",
								"type" : "GET",
								"url" : sSource,
								"data" : queryparam, // 将请求数据转换为后台要求的JSON格式
								"success" : function(json) {
									loader.noloading();
									if (json.flag == '000000') {
										dtUtil.successCallback(json,aoData,fnCallback);
									} else {
										oauth2Relogin();
									}
								}
							});

						},
						"aoColumns" : [
								{
									"sTitle" : "ISV名称",
									"bSortable" : false,
									"mData" : "isv_name",
									"bVisible" : true
								},
								{
									"sTitle" : "应用名称",
									"bSortable" : false,
									"mData" : "app_name"
								},
								{
									"sTitle" : "应用标识",
									"bSortable" : false,
									"mData" : "app_key"
								},
								{
									"sTitle" : "账号名称",
									"bSortable" : false,
									"mData" : "cust_name"
								},
								{
									"sTitle" : "账号标识",
									"bSortable" : false,
									"mData" : "cust_account"
								},
								{
									"sTitle" : "外系统编码",
									"bSortable" : false,
									"mData" : "periphery_code"
								},
								{
									"sTitle" : "扩展信息",
									"bSortable" : false,
									"mData" : "isHasExtend",
									"mRender" : function(
											data, type,
											full) {
										if (data == true)
											return "已配置";
										else if (data == false)
											return "未配置";
									}
								},
								{
									"sTitle" : "操作",
									"bSortable" : false,
									"mData" : "isHasExtend",
									"sWidth" : "15%",
									"mRender" : function(
											data,
											status,
											full) {
										var ret = '<style>.yalei2 a{ float:left;}</style><div class="btn-group yalei2" style="margin-top: 8px;">';
										ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
										ret += '<a href="javascript:void(0);" class="btn icon-edit-sign contrast" title="编辑计费规则"></a>';
										// ret += '<a
										// href="javascript:void(0);"
										// class="btn
										// icon-eraser
										// contrast"
										// title="编辑能力号码关系"></a>';
										// ret += '<a
										// href="javascript:void(0);"
										// class="btn
										// icon-cog
										// contrast"
										// title="账号号码配置"></a>';

										ret += '<div class="navbar" style="float:left;margin-top: -5px;"><ul class="nav pull-right">'
												+ '<li class="dropdown light only-icon">'
												+ '<a class="dropdown-toggle btn icon-cog contrast" data-toggle="dropdown" href="javascript:void(0);" style="line-height: 3px;"></a>'
												+ '<ul class="dropdown-menu">'
												+ '<li><a href="javascript:void(0);" class="btn icon-wrench"><span>&nbsp;账号号码配置</span></a></li>'
												+ '<li><a href="javascript:void(0);" class="btn icon-envelope"><span id="emailNoticeSpan">&nbsp;发送邮件通知</span></a></li>'
												+ '<li><a href="javascript:void(0);" class="btn icon-refresh"><span id="switchNumSpan">&nbsp;主动切换号码</span></a></li>'
												+ '</ul></li></ul></div>';

										return ret += '</div>';
									}
								} ],
						"fnDrawCallback" : function(oSettings) {
							// 表格绘制完成后，去除width属性，使表格可缩放
							$('#' + oSettings.sTableId).css("width", "");
						}
					});

				// 修改按钮
				$("#data_table tbody").on("click","tr>td>div>a.icon-reply",	
						function(e) {
							$('#accountinfo_data_table').hide();
							$("#accountForm").hide();
							$("#queryAreaId").hide();
							var rowNode = this.parentNode.parentNode.parentNode;
							var rowData = isvinfo_data_table.fnGetData(rowNode);
							var isvinforeturnform = require("./accountcompanyinforeturnform");
							isvinforeturnform.init("accountinfo_data_table_edit",rowData);
							$('#accountinfo_data_table_edit').show();
						});

				// 编辑计费规则按钮
				$("#data_table tbody").on("click","tr>td>div>a.icon-edit-sign",
						function(e) {
							$('#accountinfo_data_table').hide();
							$("#accountForm").hide();
							$("#queryAreaId").hide();
							var rowNode = this.parentNode.parentNode.parentNode;
							var rowData = isvinfo_data_table.fnGetData(rowNode);
							var isvinforeturnform = require("./ruleaccountcompanyinforeturnform");
							isvinforeturnform.init("accountinfo_data_table_edit",rowData);
							$('#accountinfo_data_table_edit').show();
							$('#accountinfo_data_table2').show();
						});
				// 账号号码配置按钮-下拉菜单
				$("#data_table tbody").on("click","tr>td>div>div>ul>li>ul>li>a.icon-wrench",
						function(e) {
							$(".dropdown-toggle").dropdown('toggle');
							$('#accountinfo_data_table').hide();
							$("#accountForm").hide();
							$("#queryAreaId").hide();
							var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							var rowData = isvinfo_data_table.fnGetData(rowNode);
							var isvinforeturnform = require("./accountinfopoolaccrel");
							isvinforeturnform.init("accountinfo_data_table_edit",rowData);
							$('#accountinfo_data_table_edit').show();
							$('#accountinfo_data_table2').show();
							return false;
						});

				// 邮件通知按钮事件-下拉菜单
				$("#data_table tbody").on("click","tr>td>div>div>ul>li>ul>li>a.icon-envelope",
						function(e) {
							$(".dropdown-toggle").dropdown('toggle');
							var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							var rowData = isvinfo_data_table.fnGetData(rowNode);
							var app_key = rowData.app_key;
							var custAccount=rowData.cust_account;
							var type = "switchNum";
							myDetail(custAccount,app_key, type);
						});

				// 主动切换号码按钮事件-下拉菜单
				$("#data_table tbody").on("click","tr>td>div>div>ul>li>ul>li>a.icon-refresh",
						function(e) {
							$(".dropdown-toggle").dropdown('toggle');
							var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							var rowData = isvinfo_data_table.fnGetData(rowNode);
							var app_key = rowData.app_key;
							var custAccount=rowData.cust_account;
							var type = "switchNum";
							myDetail(custAccount,app_key, type);
						});

				// 查看详情
				var myDetail = function(custAccount,app_key, type) {
					var my_detail = require("./accountcompanynumaccrelmodal");
					if ($("#mydetail_id").length <= 0)
						$("body").append("<div id='mydetail_id'></div>");
					my_detail.init({
						"title" : "提示",
						"renderTo" : "mydetail_id",
						"app_key" : app_key,
						"custAccount":custAccount,
						"type" : type
					});
				}

				// 查询按钮
				$('#querybtn').click(function() {
					loader.loading("数据加载中……");
					isvinfo_data_table.fnFilter("");
					return false;
				});

				// 重置按钮
				$('#restbtn').click(function() {
					loader.loading("数据加载中，请稍后……");
					$('#isvname').val("");
					$('#app_name').val("");
					$('#cust_account').val("");
					//daterangeCom.clearInput();
					isvinfo_data_table.fnFilter("");
					return false;
				});

			});

	};
});
